﻿Public Class DLGPreferences

    Dim defaultSearchField

    Public Sub Form_Load()

        On Error Resume Next

        Dim aSplitDefaultExportFields As Object

        Dim sDefaultExportFieldsList As String

        With New regOp

            .Root = regOp.RegRoot.HKEY_LOCAL_MACHINE

            .Key = "Software\bbd\AccessSearch\preferences"

            Me.TXTdefaultDatabasePath.Text = .Value("defaultDatabasePath")
            defaultSearchField = .Value("defaultSearchField")

            sDefaultExportFieldsList = .Value("defaultExportFields")

            .Key = "Software\bbd\AccessSearch\preferences\remoteDB"

            Me.TXTserverAddress.Text = .Value("serverAddress")
            Me.TXTupdatePagePath.Text = .Value("updatePagePath")
            Me.TXTfieldNames.Text = .Value("fieldNames")
            Me.TXTdatabaseName.Text = .Value("databaseName")

        End With

        buildDefaultFieldFieldsList()

        For i = 0 To Me.LISTFields.Items.Count - 1

            If defaultSearchField = Me.LISTFields.Items(i).ToString() Then

                Me.LISTFields.SelectedIndex = i

                Exit For

            End If

        Next

        aSplitDefaultExportFields = Split(sDefaultExportFieldsList, ",")

        For i = 0 To Me.LISTdefaultExportFields.Items.Count

            For j = 0 To UBound(aSplitDefaultExportFields)

                If aSplitDefaultExportFields(j) = LISTdefaultExportFields.Items(i) Then

                    LISTdefaultExportFields.SetSelected(i, True)

                    Exit For

                End If

            Next

        Next

    End Sub



    Private Sub Form_KeyUp(ByVal KeyCode As Integer, ByVal Shift As Integer)

        On Error Resume Next

        If KeyCode = 27 Then

            Close()

        End If

    End Sub


    Private Sub buildDefaultFieldFieldsList()

        On Error Resume Next

        LISTFields.Items.Clear()

        For i = 0 To FRMmain.LISTfields.Items.Count - 1

            Me.LISTFields.Items.Add(FRMmain.ListFields.Items(i))
            Me.LISTdefaultExportFields.Items.Add(FRMmain.ListFields.Items(i))

        Next

    End Sub

    Private Function validDefaultSearchField() As Boolean

        On Error Resume Next

        Dim sDefaultSearchField As String

        If Not IsNumeric(Me.LISTFields.SelectedIndex) Or Me.LISTFields.SelectedIndex < 0 Then

            Exit Function

        End If

        sDefaultSearchField = LISTFields.Items(LISTFields.SelectedIndex)

        For i = 0 To FRMmain.ListFields.Items.Count - 1

            If sDefaultSearchField = FRMmain.ListFields.Items(i) Then

                validDefaultSearchField = True

                Exit Function

            End If

        Next

    End Function

    ' Events -------------------------------------------------------------------------------------------------------------------------

    ' OK button click event -----------------------------------------------------------------------------------------------------------------------------------------

    Private Sub CMDok_Click()

        On Error Resume Next

        Dim _
            sDefaultSearchField As String, _
            sDefaultExportFieldsList As String

        Dim iConfirm

        If Not validDefaultSearchField() Then

            iConfirm = MsgBox("You have chosen an invalid default search field", vbOKCancel)

            If iConfirm <> 1 Then Exit Sub

        End If

        With New regOp

            .Root = regOp.RegRoot.HKEY_LOCAL_MACHINE
            .Key = "Software\bbd\AccessSearch\preferences"
            .Value("defaultDatabasePath") = Me.TXTdefaultDatabasePath.Text

            If IsNumeric(Me.LISTFields.SelectedIndex) And Me.LISTFields.SelectedIndex >= 0 Then

                sDefaultSearchField = LISTFields.Items(Me.LISTFields.SelectedIndex)

                If sDefaultSearchField = "" Then sDefaultSearchField = "All"

                If validDefaultSearchField() Then .Value("defaultSearchField") = sDefaultSearchField

                For i = 0 To FRMmain.ListFields.Items.Count - 1

                    If sDefaultSearchField = FRMmain.ListFields.Items(i) Then

                        FRMmain.ListFields.SelectedIndex = i

                        Exit For

                    End If

                Next

            End If

            For i = 0 To LISTdefaultExportFields.Items.Count - 1

                If LISTdefaultExportFields.SelectedIndices.Contains(i) Then

                    sDefaultExportFieldsList = sDefaultExportFieldsList & LISTdefaultExportFields.Items(i) & ","

                End If

            Next

            If Len(sDefaultExportFieldsList) > 0 Then

                sDefaultExportFieldsList = sDefaultExportFieldsList.SubString(0, Len(sDefaultExportFieldsList) - 1)

                .Value("defaultExportFields") = sDefaultExportFieldsList

            End If

            .Key = "Software\bbd\AccessSearch\preferences\remoteDB"
            .Value("serverAddress") = Me.TXTserverAddress.Text
            .Value("updatePagePath") = Me.TXTupdatePagePath.Text
            .Value("fieldNames") = Me.TXTfieldNames.Text
            .Value("databaseName") = Me.TXTdatabaseName.Text

        End With


        Close()

    End Sub

    ' Save the current column widths -------------------------------------------------------------------------------------------------------

    Private Sub CMDsaveColumnWidths_Click()

        MODpreferences.saveColumnWidths()

    End Sub

    ' Browse for choosing the default database -----------------------------------------------------------------------

    Private Sub BTNbrowse_Click()
        Throw New NotImplementedException
        '        On Error GoTo errorHandler

        '        FRMmain.CommonDialog.ShowOpen()

        '        With FRMmain.CommonDialog

        '            If .FileName <> "" Then

        '                TXTdefaultDatabasePath.Text = .FileName
        '                TXTdefaultDatabasePath.ToolTipText = .FileName

        '            End If

        '        End With

        '        buildDefaultFieldFieldsList()

        'errorHandler:

        '        If Err.Number <> 0 Then

        '            MsgBox(Err.Description & vbCrLf & Err.Number)

        '        End If

    End Sub

    ' Cancel ----------------------------------------------------------------------------------------------

    Private Sub CMDcancel_Click()

        Close()

    End Sub

    Private Sub LISTdefaultExportFields_Click()

        On Error Resume Next

        ' If 'All' is clicked, then select all of the listItems ----------------------------------------------------------------

        If LISTdefaultExportFields.SelectedIndices.Contains(0) = True Then

            For i = 1 To LISTdefaultExportFields.Items.Count - 1

                LISTdefaultExportFields.SetSelected(i, True)

            Next

        End If

    End Sub

End Class